package com.gxa.xly2024.service.impl;

import com.gxa.xly2024.dto.DTO;
import com.gxa.xly2024.mapper.EmployeeMapper;
import com.gxa.xly2024.pojo.Employee;
import com.gxa.xly2024.pojo.Purchase;
import com.gxa.xly2024.vo.VO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class EmployeeServiceImpl {


    @Resource
    private EmployeeMapper employeeMapper;


    public Map<String, Object> selectEmployeeById(Integer id) {
        Employee employee = this.employeeMapper.selectEmployeeById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);//查询layui默认0为正确
        map.put("msg", "查询id成功");
        map.put("data", employee);
        return map;
    }

    public Map<String, Object> selectEmployeeByLimit(VO vo) {
        int index = (vo.getPage() - 1) * vo.getLimit();
        DTO dto = new DTO();  // alt+enter自动补全
        // 快速copy属性到指定对象中
        BeanUtils.copyProperties(vo, dto);   // 这里代表的是，把vo和dto相同属性的值，copy过去
        dto.setIndex(index);

        List<Employee> employee = this.employeeMapper.selectEmployeeByLimit(dto);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "分页查询成功");
        map.put("count", this.employeeMapper.selectCount(dto));   // 查询当前表总条数
        map.put("data", employee);
        return map;
    }

    public Map<String, Object> insert(Employee employee) {
        this.employeeMapper.insert(employee);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "新增用户成功");
        map.put("data", employee);
        return map;
    }

    public Map<String, Object> updateById(Employee employee) {
        this.employeeMapper.updateById(employee);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "更新用户成功");
        map.put("data", employee);
        return map;
    }

    public Map<String, Object> deleteById(Integer id) {
        this.employeeMapper.deleteById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "删除用户成功");
        map.put("id", id);
        return map;
    }
}
